// packageE/appointment/Dividends/AppointmentApplyClass.js
const app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    projectName: "",
    selshow: false,
    show: false,
    columns: ["分类一", "上面服务"],
    level: [{
        name: "省级代理",
        tag: 1
      },
      {
        name: "市级代理",
        tag: 2
      },
      {
        name: "区/县级代理",
        tag: 3
      }
    ],
    status:1,
    imgFail: "https://mini-app-img-1251768088.cos.ap-guangzhou.myqcloud.com/images%2FagentShop%2F%E7%AD%89%E5%BE%85%E5%AE%A1%E6%A0%B8%402x%20(1).png"
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    this.getInfo();
    this.projectClass();
    this.initData();
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  },
  Reapply(){
    this.setData({
      status:1
    })
  },
  async getInfo(){
    let {
      data
    } = await app._postNetWork({
      url: app.getNetAddresss("plugin.appointment.frontend.member.get-info"),
    });
    if(data.result){
      if(data.data.is_agent_apply.status == 4){
        this.setData({
          status:0
        })
      }
    }
  },
  async tapSubmit() {
    if (!this.data.projectName) {
      app.tips("请先选择项目");
      return;
    }
    let json = {
      form: {
        category_id: this.data.classArr[this.data.projectSelectInd].id,
        area_id: "",
        area_level: this.data.selectLevel.tag
      }
    };
    if (this.data.selectLevel.tag == 1) {
      if (!this.data.provicevalue_id) {
        app.tips("请先选择所代理区域");
        return;
      }
      json.form.area_id = this.data.provicevalue_id;
    } else if (this.data.selectLevel.tag == 2) {
      if (!this.data.cityvalue_id) {
        app.tips("请先选择所代理区域");
        return;
      }
      json.form.area_id = this.data.cityvalue_id;
    } else if (this.data.selectLevel.tag == 3) {
      if (!this.data.regionvalue_id) {
        app.tips("请先选择所代理区域");
        return;
      }
      json.form.area_id = this.data.regionvalue_id;
    } else if (this.data.selectLevel.tag == 4) {
      if (!this.data.streetvalue_id) {
        app.tips("请先选择所代理区域");
        return;
      }
      json.form.area_id = this.data.streetvalue_id;
    }
    let {
      data
    } = await app._postNetWork({
      url: app.getNetAddresss("plugin.appointment.frontend.category-agent.apply"),
      data:json,
    });
    if(data.result == 1){
      wx.showModal({
        title: '',
        content: data.msg,
        showCancel:false,
        complete: (res) => {
          if (res.confirm) {
            wx.navigateBack({
              delta: 1,
            })
          }
        }
      })      
    }else{
      app.tips(data.msg)
    }
  },
  // 初始化数据
  initData() {
    this.getAddressDataInfo(); // 获取地址json数据
  },
  async getAddressDataInfo() {
    let {
      data
    } = await app._getNetWork({
      url: app.getNetAddresss("member.member-address.address")
    });
    if (!data.result) {
      app.tips(data.msg)
      return
    }
    this.setData({
      provinceoptions: data.data.province,
      citys: data.data.city,
      district: data.data.district,
    });
  },
  showOpen() {
    this.setData({
      show: true
    })
  },
  selshowOpen() {
    this.setData({
      selshow: true
    })
  },
  onClose() {
    this.setData({
      selshow: true
    })
  },
  async projectClass() {
    let {
      data
    } = await app._getNetWork({
      url: app.getNetAddresss("plugin.appointment.frontend.project-category.get-list")
    });
    let res = data;
    if (res.data.length > 0) {
      let array = [];
      for (let i = 0; i < res.data.length; i++) {
        array.push(res.data[i].name);
      }
      this.setData({
        columns: array,
        classArr: res.data
      })
    }
  },
  onChange(e) {
    console.log(e)
  },
  onClickRight(e) {
    this.setData({
      show: false,
      projectName: e.detail.value,
      projectSelectInd: e.detail.index
    })
  },
  onClickLeft() {
    this.setData({
      show: false,
      selshow: false,
      cityshow: false,
      proshow: false,
      regshow: false,
      streetshow: false
    })
  },
  selectProviceHandleShow() {
    this.setData({
      proshow: true,
    });
  },
  getlevelVal(e) {
    this.setData({
      selectLevel: e.target.dataset.item || e.currentTarget.dataset.item,
    });
    this.setData({
      selshow: false,
    });
    let name = this.data.selectLevel;
    if (name.tag == "1") {
      this.setData({
        prozoneshow: true,
        cityzoneshow: false,
        regzoneshow: false,
        // strshow: false,
      });
    } else if (name.tag == "2") {
      this.setData({
        prozoneshow: true,
        cityzoneshow: true,
        regzoneshow: false,
        // strshow: false,
      });
    } else if (name.tag == "3") {
      this.setData({
        prozoneshow: true,
        cityzoneshow: true,
        regzoneshow: true,
        // strshow: false,
      });
    } else if (name.tag == "4") {
      this.setData({
        prozoneshow: true,
        cityzoneshow: true,
        regzoneshow: true,
        // strshow: true,
      });
    }
  },
  // 选择省份的响应方法
  selectProviceHandle(value) {
    // 初始化一些默认值 避免逻辑错误
    console.log(value.currentTarget.dataset.item, "items");
    this.setData({
      provicevalue: value.currentTarget.dataset.item.areaname || value.target.dataset.item.areaname,
      proshow: false,
      proviceObject: value.currentTarget.dataset.item || value.target.dataset.item,
      cityvalue: '',
      regionvalue: '',
      streetvalue: '',
      provicevalue_id: '',
      cityvalue_id: '',
      regionvalue_id: '',
      streetvalue_id: ''
    });
    let citys = this.data.citys;
    let arr = [];
    var id = value.currentTarget.dataset.item.id || value.target.dataset.item.id;
    this.setData({
      provicevalue_id: value.currentTarget.dataset.item.id || value.target.dataset.item.id
    })
    console.log(id, "id878778", this.data.provicevalue);
    for (let i = 0; i < citys.length; i++) {
      if (citys[i].parentid == id) {
        arr.push(citys[i]);
        this.setData({
          cityoptions: arr,
        });
      }
    }
    console.log(this.data.cityoptions, "城市");
  },
  // 弹窗显示省 市 区数据
  showCity() {
    if (this.data.provicevalue == "") {
      wx.showToast({
        title: "请选择省级区域",
        icon: "none",
      });
      return;
    }
    this.setData({
      cityshow: true,
    });
  },
  showReg() {
    if (this.data.provicevalue == "") {
      wx.showToast({
        title: "请选择省级区域",
        icon: "none",
      });
      return;
    }
    if (this.data.cityvalue == "") {
      wx.showToast({
        title: "请选择市级区域",
        icon: "none",
      });
      return;
    }
    this.setData({
      regshow: true,
    });
  },
  showStr() {
    if (this.data.provicevalue == "") {
      wx.showToast({
        title: "请选择省级区域",
        icon: "none",
      });
      return;
    }

    if (this.data.cityvalue == "") {
      wx.showToast({
        title: "请选择市级区域",
        icon: "none",
      });
      return;
    }

    if (this.data.regionvalue == "") {
      wx.showToast({
        title: "请选择区/县级区域",
        icon: "none",
      });
      return;
    }
    this.setData({
      streetshow: true,
    });
  },
  // 选择县区的响应方法
  selectRegionHandle(value) {
    console.log(value.currentTarget.dataset.item, "items");
    let val = value.currentTarget.dataset.item || value.target.dataset.item;
    let {
      id
    } = value.currentTarget.dataset.item;
    this.setData({
      regionvalue: val.areaname,
      regshow: false,
      regionObject: val,
      regionvalue_id: id,
    });
    this.isStreetShow(val.id);
  },
  // 选择市的响应方法
  selectCityHandle(value) {
    console.log(value.currentTarget.dataset.item, "items");
    this.setData({
      cityvalue: value.currentTarget.dataset.item.areaname || value.target.dataset.item.areaname,
      cityshow: false,
      cityObject: value.currentTarget.dataset.item || value.target.dataset.item,
    });
    let citys = this.data.district;
    let arr = [];
    var id = value.currentTarget.dataset.item.id || value.target.dataset.item.id;
    this.setData({
      cityvalue_id: id
    })
    console.log(id, "id878778");
    for (let i = 0; i < citys.length; i++) {
      if (citys[i].parentid == id) {
        arr.push(citys[i]);
        this.setData({
          regionoptions: arr,
        });
      }
    }
    console.log(this.data.regionoptions, "64454454554城市");
  },
  isStreetShow(value) {
    if (this.data.selectLevel.tag != 4) {
      return;
    }
    let urlStr = app.getNetAddresss("member.memberAddress.street");
    urlStr += "&district_id=" + value;
    app._getNetWork({
      url: urlStr,
      success: (res) => {
        if (res.data.result === 1) {
          console.log(res.data, "数据");

          this.setData({
            stroptions: res.data.data,
            // strshow: true,
          });
        } else {
          this.setData({
            // strshow: false,
          });
          wx.showToast({
            title: res.data.msg,
            icon: "none",
          });
        }
      },
      fail: function (res) {
        console.log(res);
      },
    });
  },
  selectStrionHandle(value) {
    var Val = value.target.dataset.item || value.currentTarget.dataset.item;
    this.setData({
      districtObject: Val,
      streetvalue: Val.areaname,
      streetvalue_id: Val.id,
      streetshow: false,
    });
  },
})